package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;

/**
 * Created by 80002023 on 2016/7/25.
 */
public class FirstGame extends ApplicationAdapter {

    private Pixmap mPixmap;
    private Texture mTexture;
    private Mesh mMesh;
    private BitmapFont mBitmapFont;
    private SpriteBatch mSpriteBatch;
    private OrthographicCamera mCamera;
    private ShaderProgram shader;

    @Override
    public void create() {
        mPixmap = new Pixmap(Gdx.files.internal("image1.jpg"));
        // 可以直接操作位图添加矩形，这里的坐标是y轴向下的
        mPixmap.setColor(Color.BLUE);
        mPixmap.drawRectangle(0, 0, 100, 100);
        mTexture = new Texture(mPixmap);
        mMesh = createTriangle();
        mBitmapFont = new BitmapFont(Gdx.files.internal("test.fnt"),
                Gdx.files.internal("test.png"), false);
        mSpriteBatch = new SpriteBatch();
        // 镜头需要设定宽高和镜头中间位置
        mCamera = new OrthographicCamera(800, 480);
        mCamera.position.set(800 / 2, 480 / 2, 0);

        // 以下命令供GPU使用(不支持GLES2.0就不用跑了)
        String vertexShader = "attribute vec4 a_position;   /n"
                + "attribute vec2 a_texCoord;   /n"
                + "varying vec2 v_texCoord;     /n"
                + "void main()                  /n"
                + "{                            /n"
                + "   gl_Position = a_position; /n"
                + "   v_texCoord = a_texCoord;  /n"
                + "}                            /n";
        String fragmentShader = "#ifdef GL_ES/n"
                + "precision mediump float;/n"
                + "#endif/n"
                + "varying vec2 v_texCoord;                            /n"
                + "uniform sampler2D s_texture;                        /n"
                + "uniform sampler2D s_texture2;                        /n"
                + "void main()                                         /n"
                + "{                                                   /n"
                + "  gl_FragColor = texture2D( s_texture, v_texCoord ) * texture2D( s_texture2, v_texCoord);/n"
                + "}                                                   /n";
        shader = new ShaderProgram(vertexShader, fragmentShader);


        this.resize(480, 480);
    }

    public Mesh createTriangle() {
        Mesh mesh = new Mesh(true, 3, 3, new VertexAttribute(VertexAttributes.Usage.Position, 3, "a_position"));

        mesh.setVertices(new float[]{100f, 100f, 0, 50f, 50f, 0, 150f, 50f, 0});
        mesh.setIndices(new short[]{0, 1, 2});
        return mesh;
    }

    @Override
    public void render() {
        // 镜头的更新与设置矩阵到SpriteBatch
        mCamera.update();
        mSpriteBatch.setProjectionMatrix(mCamera.combined);

        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        mSpriteBatch.begin();
        // opengl默认的坐标系是左下角为原点的， 绘制的纹理也是以左下角为起点计算的
        mSpriteBatch.draw(mTexture, 0, 0);

//        mBitmapFont.setFixedWidthGlyphs();
        mBitmapFont.getData().setScale(1.25f,1.25f);
        mBitmapFont.draw(mSpriteBatch, "周末了,happy", 150, 100);
        mSpriteBatch.end();
        // 开始使用ShaderProgram渲染
//        shader.begin();
//        shader.setUniformi("s_texture", 0);
//        shader.setUniformi("s_texture2", 1);
        // 绘制一个三角形
//        mMesh.render(shader, GL20.GL_TRIANGLES, 0, 3);
        // 结束ShaderProgram渲染
//        shader.end();
    }

    @Override
    public void dispose() {
        // 所有实现Disposable接口的都需要释放资源
        mPixmap.dispose();
        mTexture.dispose();
        mSpriteBatch.dispose();
    }

    @Override
    public void resize(int width, int height) {
        super.resize(width, height);
    }
}
